package com.wanglibao.woodtrade.woodtrade.ui.activity.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.facebook.drawee.view.SimpleDraweeView;
import com.wanglibao.woodtrade.woodtrade.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zy on 2016/5/11.
 */
public class ItemRvEditCommodityAdapter extends RecyclerView.Adapter<ItemRvEditCommodityAdapter.MyViewHolder> {

    private List<String> mList = new ArrayList<>();

    private Context context;
    private LayoutInflater layoutInflater;
    

    public ItemRvEditCommodityAdapter(List<String> list, Context context) {
        mList = list;
        this.context = context;
        this.layoutInflater =LayoutInflater.from(context) ;
    }
    public void removeData(int position)
    {
        mList.remove(position);
        notifyItemRemoved(position);
    }
    public void addData(int position,String string)
    {
        mList.add(position, string);
        notifyItemInserted(position);
    }
    public interface OnItemClickLitener
    {
        void onItemClick(View view, int position);
        void onItemLongClick(View view, int position);
    }

    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener)
    {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder holder = new MyViewHolder(LayoutInflater.from(
                context).inflate(R.layout.item_imgv_rv_commodity, parent,
                false));
        return holder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        holder.mSimpleDraweeView.setImageURI(Uri.parse(mList.get(position)));
//        Picasso.with(context).load(mList.get(position)).into(holder.mSimpleDraweeView);
        // 如果设置了回调，则设置点击事件
        if (mOnItemClickLitener != null)
        {
            holder.itemView.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemClick(holder.itemView, pos);
                }
            });

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener()
            {
                @Override
                public boolean onLongClick(View v)
                {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemLongClick(holder.itemView, pos);
                    return false;
                }
            });
        }
    
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder
    {

        SimpleDraweeView mSimpleDraweeView;

        public MyViewHolder(View view)
        {
            super(view);
            mSimpleDraweeView = (SimpleDraweeView) view.findViewById(R.id.sdv_item_rv);
        }
    }
    
}
